package com.wntms.wgj.service.impl;

import com.github.pagehelper.PageHelper;
import com.wntms.wgj.dao.FreightSettleDao;
import com.wntms.wgj.pojo.FreightSettles;
import com.wntms.wgj.service.FreightSettleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class FreightSettleServiceImpl implements FreightSettleService {

    @Autowired
    private FreightSettleDao freightSettleDao;

    @Override
    public List<FreightSettles> findFreightSettles() {
        List<FreightSettles> list = freightSettleDao.findFreightSettles();
        for (FreightSettles freightSettle : list) {
            Double money = freightSettle.getFreightContracts().getFare()+freightSettle.getFreightContracts().getInsurancefare();
            BigDecimal bigDecimal = new BigDecimal(money);
            double numm = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            freightSettle.setMoney(numm);
            freightSettle.setOnemoney(numm);
        }
        return list;
    }


    @Override
    public List<FreightSettles> findByPage(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize);

        List<FreightSettles> list = freightSettleDao.findFreightSettles();
        for (FreightSettles freightSettle : list) {
            Double money = freightSettle.getFreightContracts().getFare()+freightSettle.getFreightContracts().getInsurancefare();
            BigDecimal bigDecimal = new BigDecimal(money);
            double numm = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            freightSettle.setMoney(numm);
            freightSettle.setOnemoney(numm);
        }

        return list;
    }

    @Override
    public boolean updateSettle(FreightSettles freightSettles) {
        return freightSettleDao.updateSettle(freightSettles) == 1 ? true:false;
    }

    @Override
    public boolean deleteSettle(Integer id) {
        return freightSettleDao.deleteSettle(id) == 1 ? true:false;
    }

    @Override
    public FreightSettles findByFId(String fid) {
        FreightSettles fs = freightSettleDao.findByFid(fid);

        Double money = fs.getFreightContracts().getFare()+fs.getFreightContracts().getInsurancefare();
        BigDecimal bigDecimal = new BigDecimal(money);
        double numm = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
        fs.setMoney(numm);
        fs.setOnemoney(numm);

        return fs;
    }
}
